﻿using CommonMethod;
using SK3000RunningTestTool.SKService;
using SK3000RunningTestTool.SKServerSource;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace SK3000RunningTestTool
{
    public partial class FrmSystemSetting : DevComponents.DotNetBar.Metro.MetroForm
    {
        public FrmSystemSetting()
        {
            InitializeComponent();
        }

        private void FrmSystemSetting_Load(object sender, EventArgs e)
        {
            ServiceTestTypeSource.Init();
            Init();
        }

        public void Init()
        {
            List<CommonMethod.CommonObject.ComboBoxItem> lstSource = new List<CommonMethod.CommonObject.ComboBoxItem>();
            cmbServiceType.DataSource = ServiceTestTypeSource.ServiceTestTypeList.Values.ToList();
            cmbServiceType.DisplayMember = "ServiceTestTypeName";
            Init_GetServiceTestList();
        }

        public bool Init_GetServiceTestList()
        {
            bool bolResult = false;
            panel2.Controls.Clear();
            ServiceTestSource.Init();
            foreach (ISKService item in ServiceTestSource.ServiceTestList)
            {
                SetControl(item);
            }
            return bolResult;
        }

        public void SetControl(ISKServiceStruct serviceTest)
        {
            CustomControl.ServiceTestInfoControl cServiceTestInfo =new  CustomControl.ServiceTestInfoControl();
            cServiceTestInfo.BackColor = System.Drawing.Color.White;
            cServiceTestInfo.Location = new System.Drawing.Point(482, 25);
            cServiceTestInfo.Name = "cServiceTest" + serviceTest.ServiceName;
            cServiceTestInfo.Size = new System.Drawing.Size(261, 34);
            cServiceTestInfo.Dock = DockStyle.Top;
            cServiceTestInfo.SetServiceTestType(serviceTest);
            cServiceTestInfo.ServiceTestInfoDeleteEvent += CServiceTestInfo_ServiceTestInfoDeleteEvent;
            panel2.Controls.Add(cServiceTestInfo);

        }

        private void CServiceTestInfo_ServiceTestInfoDeleteEvent(object sender, object ServiceTestInfoDeleteValue)
        {
            ISKServiceStruct ServiceTest = (ISKServiceStruct)sender;
            ISKServiceStruct ServiceTest1 = new ServiceStruct
            {
                ID = ServiceTest.ID,
                ServiceAddress = ServiceTest.ServiceAddress,
                ServiceName = ServiceTest.ServiceName,
                ServicePort = ServiceTest.ServicePort,
                TypeID = ServiceTest.TypeID,
            };
            bool bolResult= DeleteNodeInfo(ServiceTest1, Parameter.XMLDataFilePath, Parameter.XMLParentNodeName);
            Init_GetServiceTestList();
        }

        private void cmbServiceType_SelectedValueChanged(object sender, EventArgs e)
        {
            ServiceTestType value = (ServiceTestType)cmbServiceType.SelectedValue;
            txtServiceTestName.Text = value.ServiceTestTypeName;
            txtServiceAddress.Text = value.ServiceAddress;
            txtServicePort.Text = Convert.ToString(value.ServicePort);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ServiceTestType value = (ServiceTestType)cmbServiceType.SelectedValue;
            ISKServiceStruct ServiceTest = new ServiceStruct
            {
                ID= ServiceTestSource.GetID(),
                ServiceAddress= txtServiceAddress.Text,
                ServiceName= txtServiceTestName.Text,
                ServicePort= Convert.ToUInt16(txtServicePort.Text.Trim()),
                TypeID= value.TypeID,
            };
            AddNodeInfo(ServiceTest, Parameter.XMLDataFilePath, Parameter.XMLParentNodeName);
            Init_GetServiceTestList();
        }

    

        public static bool AddNodeInfo<T>(T t, string strFilePath, string strParentName)
        {
            bool bolResult = true;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(strFilePath);
            XmlNode ParentNode = xmlDoc.SelectSingleNode(strParentName);   //父节点
            XmlElement element = xmlDoc.CreateElement(typeof(T).Name);
            string tempName = "";
            PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
            foreach (PropertyInfo pi in propertys)
            {
                tempName = pi.Name;
                if (!pi.CanWrite)
                {
                    continue;
                }
                object obj = pi.GetValue(t, null);
                if (pi.PropertyType.IsEnum)
                {
                    obj = obj.GetHashCode();
                }
                element.SetAttribute(pi.Name, Convert.ToString(obj));
            }
            ParentNode.AppendChild(element);
            xmlDoc.Save(strFilePath);
            return bolResult;
        }

        public static bool DeleteNodeInfo<T>(T t, string strFilePath, string strParentName)
        {
            bool bolResult = false;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(strFilePath);
            XmlNode ParentNode = xmlDoc.SelectSingleNode(strParentName);   //父节点
            PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
            XmlNode nodeDelete = null;
            foreach (XmlNode node in ParentNode.ChildNodes)
            {
                if (NodeEqual(t, node, propertys))
                {
                    nodeDelete = node;
                    break;
                }
            }
            if (nodeDelete != null)
            {
                ParentNode.RemoveChild(nodeDelete);
                xmlDoc.Save(strFilePath);
                bolResult = true;
            }
            return bolResult;
        }
        private static bool NodeEqual<T>(T t, XmlNode node, PropertyInfo[] propertys)
        {
            string tempName = "";
            foreach (PropertyInfo pi in propertys)
            {
                if (!pi.CanWrite)
                {
                    continue;
                }
                tempName = pi.Name;
                if (node.Attributes[tempName] == null)
                {
                    return false;
                }
                object Temp_objValue = pi.GetValue(t, null); 
                if (pi.PropertyType.IsEnum)
                {
                    Temp_objValue = Temp_objValue.GetHashCode();
                }
                string Temp_strValue = Convert.ToString(Temp_objValue);
                if (node.Attributes[tempName].Value != Temp_strValue)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
